//
//  YPZQSureViewController.m
//  ChengHuiTong
//
//  Created by CHT-Technology on 2017/2/20.
//  Copyright © 2017年 张文权. All rights reserved.
//

#import "YPZQSureViewController.h"

@interface YPZQSureViewController (){
    
    NSDictionary *_dataDic;
}

@end

@implementation YPZQSureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"债权转让";
    
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self setUpData];
}

- (void)setUpData{
    
    [YPHttpRequestMethod sendAsyRequestURL:CMD_ZQZRBUY withData:@{@"tid":REALSTRING(_tid),@"token":TOKEN} withCompletionBlock:^(NSDictionary *responseDictionary) {
        
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        if ([responseDictionary[@"status"] intValue]== 0) {
            
            _dataDic = responseDictionary;
            if ([_dataDic[@"hs_flag"] integerValue] == 1) {
                self.view.subviews.count >0 ?: [self setUpUI];
            }else{
                [self showHsBoxWithFlag:[_dataDic[@"hs_flag"] integerValue]];
            }
        }
        
    } withFailedBlock:^(NSError *error) {
        
    }withIndicator:self.view.subviews.count > 0?NO:YES  atView:self.view];
}

- (void)setUpUI{
    
    [self.view addSubview:[self getContentViewWithDataArray:@[@"项目名称    ",@"剩余期限    ",@"还款方式    "]
                                                changeArray:@[s_Sub(_dataDic[@"title"], 15),
                                                              [NSString stringWithFormat:@"%@期",_dataDic[@"remain_periods"]],
                                                              YPGetRepayments(_dataDic[@"style"])]
                                                    isMoney:NO]];
    
    [self.view addSubview:[self getContentViewWithDataArray:@[
                                                              @"认购金额    ",
                                                              @"应收本息    ",
                                                              @"预期收益    ",
                                                              @"账户余额    "
                                                              ]
                                                changeArray:@[
                                                              [NSString stringWithFormat:@"%@元",_dataDic[@"sale_price"]],
                                                              [NSString stringWithFormat:@"%@元",_dataDic[@"credit_value"]],
                                                              [NSString stringWithFormat:@"%@元",_dataDic[@"will_income"]],
                                                              [NSString stringWithFormat:@"%@元",_dataDic[@"money_usable"]]
                                                              ]
                                                    isMoney:YES]
     ];
    
    UIButton *sureBtn = [UIButton creatWithFrame:CGRectMake(autoScaleW(30), autoScaleH(260), SCREEN_WIDTH - autoScaleW(60), autoScaleH(40))
                                            font:[UIFont systemFontOfSize:15]
                                            text:@"确定"
                                          action:@selector(sureClick)
                                          target:self];
    sureBtn.backgroundColor = kColor_AppNavColor;
    [self.view addSubview:sureBtn];
    
}

- (UIView *)getContentViewWithDataArray:(NSArray *)dataArray
                            changeArray:(NSArray *)changeArray
                                isMoney:(BOOL)isMoney{
    
    UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, isMoney?autoScaleH(116):autoScaleH(8), SCREEN_WIDTH, isMoney?autoScaleH(130):autoScaleH(100))];
    contentView.backgroundColor = [UIColor whiteColor];
    for (int i = 0; i < dataArray.count; i ++) {
        UILabel *label = [UILabel creatWithFrame:CGRectMake(leftMargin, autoScaleH(30)*i + autoScaleH(5), SCREEN_WIDTH - leftMargin*2, autoScaleH(30))
                                            font:[UIFont systemFontOfSize:12]
                                            text:dataArray[i]
                                       textColor:kColor_greyAshTitleColor
                                   textAlignment:0];
        label.attributedText = [self getAttributedStringWithChangeTextColor:isMoney?kColor_AppNavColor:[UIColor blackColor]
                                originalString:[NSString stringWithFormat:@"%@%@",dataArray[i],changeArray[i]] changeString:changeArray[i]];
        [contentView addSubview:label];
    }
    [contentView addLine];
    return contentView;
}

- (NSAttributedString *)getAttributedStringWithChangeTextColor:(UIColor *)textColor
                                                originalString:(NSString *)originalString
                                                  changeString:(NSString *)changeString
{
    
    NSMutableAttributedString *mAttString = [[NSMutableAttributedString alloc]initWithString:originalString];
    [mAttString addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],
                                NSForegroundColorAttributeName:textColor,
                                } range:[originalString rangeOfString:changeString]];
        return mAttString;
}

- (void)sureClick{
    if ([_dataDic[@"money_usable"] doubleValue] < [_dataDic[@"sale_price"] doubleValue]) {
        
        [MBProgressHUD showError:@"认购金额不能大于账户余额" toView:self.view];
        return;
    }
    //确定认购债权
    YPGoWebView([NSString stringWithFormat:@"%@/index.php?m=app&c=hsbank&a=transferTender&token=%@&tid=%@",kAppHostAddress,TOKEN,_tid], self.navigationController);
}



@end
